<?php

namespace app\admin\controller;

use think\App;

class Banner extends Base
{

    protected $banner;

    public function __construct(App $app, \app\common\model\Banner $banner)
    {
        parent::__construct($app);
        $this->banner = $banner;
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $result = $this->banner->order('id', 'desc')->paginate(10);
            ajaxSuccess($result);
        }
        return $this->fetch();
    }

    public function save()
    {
        if (isAjax()) {
            $data = $this->request->post(['image' => '', 'id'=>'','sort'=>'']);
            if (!$data['image']) {
                ajaxError('图片不能为空');
            }
            if (isset($data['id']) && $data['id'] > 0) {
                $result = $this->banner->update($data);
            } else {
                $result = $this->banner->save($data);
            }
            if (!$result)
                ajaxError('保存失败');
            ajaxSuccess();
        }
    }

    public function delete()
    {
        if (isAjax()) {
            $id = input('post.id');
            $result = $this->banner::destroy($id);
            if ($result) {
                ajaxSuccess();
            }
        }
        ajaxError();
    }


}

